package com.ruisoft.business.employee.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sun.misc.BASE64Decoder;

import com.ruisoft.base.bean.QueryData;
import com.ruisoft.base.dao.BaseDao;
import com.ruisoft.base.service.BaseService;
import com.ruisoft.core.param.CacheDataFactory;
@Service
public class EmployeeService extends BaseService {

    @Autowired
    public BaseDao baseDao;

    @Autowired
    private CacheDataFactory cacheData;
    
    
    public List<JSONObject> searchForPage(QueryData q) throws Exception {
        List<JSONObject> itemData = search(q, "employee.select.employeeinfo.query");
        return itemData;
    }
    
    public JSONObject searchUser(String userid,HttpServletRequest request) throws Exception{
    	List<JSONObject> userDataList = baseDao.queryId(userid, "employee.select.employeeinfo.userid");
    	JSONObject userdata = new JSONObject();
    	if(userDataList.size()>0){
    		userdata = userDataList.get(0);
    		if(userdata.has("image")){
    			String imagestr=(String)userdata.get("image");
        		if(JImgPic(imagestr,request)){
        			System.out.println("保存图片成功");
        		}else{
        			System.out.println("保存图片失败");
        		}
    		}
    	}
    	return userdata;
    }

	//解码过程
	public static boolean JImgPic(String imgStr,HttpServletRequest request){
		if(imgStr == null){
			return false;
		}
		BASE64Decoder decoder = new BASE64Decoder();
		try{
			//Base64解码
			byte[] b = decoder.decodeBuffer(imgStr);
			for(int i=0;i<b.length;i++){
				if(b[i]<0){
					//调整异常数据
					b[i]+=256;
				}
			}
			//生成jpg图片
			String url = request.getSession().getServletContext().getRealPath("\\")+"images";
			String imgFilePath=url+File.separator+"sfz.jpg";//新生成的图片

			OutputStream out = new FileOutputStream(imgFilePath);
			out.write(b);
			out.flush();
			out.close();
			return true;
		}catch(Exception e){
			return false;
		}
	}
    public JSONObject saveEmployee(JSONObject jsonData) throws Exception {
        jsonData.put("verify_method", "15");
        jsonData.put("user_valid", "1");
        baseDao.add(jsonData, "employee.add.employeeinfo.add");
        return jsonData;
    }
    
    public JSONObject saveFace(JSONObject jsonData) throws Exception {
        baseDao.add(jsonData, "employee.add.faceInfo.add");
        return jsonData;
    }
    
    public JSONObject saveMachUser(JSONObject jsonData) throws Exception {
        baseDao.add(jsonData, "employee.add.machuser.add");
        return jsonData;
    }
    
    public JSONObject updateEmployee(JSONObject jsonData) throws Exception {
        baseDao.update(jsonData, "employee.update.updateEmployeeinfo");
        return jsonData;
    }
    
    public JSONObject updateEmployeeValid(JSONObject jsonData) throws Exception {
        baseDao.update(jsonData, "employee.update.updateEmployee.valid");
        return jsonData;
    }
    
    public int deleteEmployee(String id) throws Exception{
        int status = 0;
        status = baseDao.deleteCode(id,"employee.delete.deleteEmployee");
        return status;
    }
}
